#pragma once

namespace game
{

//move to game / seperate h file
	class DoodadManager
	{
		 
	public:

		std::vector<game::IEntity *> _entity;
		
		void Add(game::IEntity * ent)
		{
			_entity.push_back(ent);
		}

		void Draw()
		{
			for each (game::IEntity* ent in _entity)
			{
				//if in window
				ent->Draw();
			}
		}
	};
	class UnitManager
	{
		 std::vector<game::Unit *> _unit;
		 int db;

	public:

		UnitManager()
		{
			//_unit.reserve(20);
		}

		~UnitManager()
		{
			const int size = _unit.size();
			for (int i = 0; i < size; i++)	//enable after refactoring
			{
				delete _unit[i];
			}
		}
		
		
		void Add(game::Unit * ent)
		{
			ent->Init();
			_unit.push_back(ent);
			
		}

		
		void Update()
		{
			for each (game::Unit* unit in _unit)
			{
				unit->Update();
			}
		}

		void Draw()
		{
			for each (game::Unit* unit in _unit)
			{
				//if in window
				unit->Draw();
			}
		}
		Unit * get_unit(std::string name)
		{
			const size_t size = _unit.size();
			for (size_t i = 0; i < size; i++)
			{
				if (_unit.at(i)->get_name() == name)
				{
					return _unit.at(i);
				}
			}
			return NULL;
		}
		Unit* get_closest_unit(sf::Vector2f pos)
		{
			const size_t size = _unit.size();
			Unit * closest_unit = NULL;
			float max_dist = 99999999999.9;
			for (size_t i = 0; i < size; i++)
			{
				float dx = pos.x - _unit.at(i)->get_position().x;
				float dy = pos.y - _unit.at(i)->get_position().y;
				float dist = dx*dx+dy*dy;
				if (dist < max_dist)
				{
					max_dist = dist;
					closest_unit = _unit.at(i);
				}
			}
			return closest_unit;
		}
		Unit* get_closest_unit(Unit * unit)
		{
			sf::Vector2f pos = unit->get_position();
			const int size = _unit.size();
			Unit * closest_unit = NULL;
			float max_dist = 99999999999.9;
			if (!_unit.empty())
			for (int i = 0; i < size; i++)
			{
				float dx = pos.x - _unit.at(i)->get_position().x;
				float dy = pos.y - _unit.at(i)->get_position().y;
				float dist = dx*dx+dy*dy;
				if (dist < max_dist && _unit.at(i) != unit)
				{
					max_dist = dist;
					closest_unit = _unit.at(i);
				}
			}
			if (closest_unit == NULL){std::cout << "Returing null closest unit" << std::endl;}
			return closest_unit;
		}
	};

}